package team.tunan.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import team.tunan.entity.Score;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author sqx
 * @since 2024-01-01
 */
public interface ScoreMapper extends BaseMapper<Score> {
    @Select("select sid, player_id, mid, jid, votes, judges_scoring, total_score, win, match_id, roundNum, status from score where player_id =#{score.playerId} and mid = #{score.mid} and status = 0")
    List<Score> getScore(@Param("score") Score score);

    /**
     * 修改评委打分状态
     * @param mid
     * @return
     */
    @Update("update score set status = 1 where mid  = #{mid} and player_id = #{pid}")
    int updateStatus(@Param("mid") Integer mid, @Param("pid") Integer pid);

    // 查询score 表 根据jid , pid  ,mid 查看 该评委是否给id 为 mid的对战的 pid 歌手打分
    @Select("select jid from score where player_id =#{score.playerId} and mid = #{score.mid}")
    Integer getJScore(@Param("score") Score score);


    // 开启比赛后 score表 新增歌手 获取两个歌手的id列表  插入数据库
    int addPlayerScore(@Param("scores") List<Score> scores);

    // 评委打分
    int addJudgesScore(@Param("score") Score score);

    // 获取选手得分情况
    @Select("select  sid, player_id, mid, jid, votes, judges_scoring, total_score, win, match_id, roundNum, status from score where player_id = #{pid} and match_id = #{matchId} and roundNum = #{roundNum}")
    Score getVotesByPid(@Param("pid") Integer pid, @Param("matchId") Integer matchId, @Param("roundNum") Integer roundNum);

    // 获取歌手票数
    @Select("select votes from score where player_id = #{pid} and mid = #{mid}")
    Integer getVotesByPidAndMid(@Param("pid") Integer pid, @Param("mid") Integer mid);

    // 变更歌手票数
    @Update("update score set votes = #{votes} where mid = #{mid} and player_id = #{playerId}")
    Integer updateVotesByMidAndPlayerId(@Param("votes") int votes, @Param("mid") Integer mid, @Param("playerId") Integer playerId);


    // 插入总分
    @Update("update score set total_score = #{totalScore} where player_id =#{pid}")
    int updateTotalScore(@Param("totalScore") double totalScore, @Param("pid") Integer pid);

    // 更新win 是否获胜
    @Update("update score set win = 1 where player_id = #{pid}")
    int updateWin(@Param("pid") Integer pid);

    // 查询 win = 1 的歌手
    @Select("select sid, player_id, mid, jid, votes, judges_scoring, total_score, win, match_id, roundNum, status from score where win =1 ORDER BY total_score DESC ")
    List<Map<String, Object>> getWins();

    @Select("select sid, player_id, mid, jid, votes, judges_scoring, total_score, win, match_id, roundNum, status from score where win =1 and match_id = #{gid} and roundNum = #{roundNum} ORDER BY total_score DESC ")
    List<Map<String, Object>> getWinsByGameAndRound(@Param("gid") Integer gid,@Param("roundNum") Integer roundNum);

}
